#coding:UTF-8
import numpy as np
import svm

def load_data_libsvm(data_file):
    '''
    导入训练数据
    :param data_file: 训练数据所在文件
    :return: data: 训练样本的特征, label: 训练样本的标签
    '''
    data = []
    label = []
    f = open(data_file)
    for line in f.readlines():
        lines = line.strip().split(' ')

        label.append(float(lines[0]))

        index = 0
        tmp = []
        for i in range(1, len(lines)):
            li = lines[i].strip().split(":")
            if int(li[0]) - 1 == index:
                tmp.append(float(li[1]))
            else:
                while int(li[0]) -1 > index:
                    tmp.append(0)
                    index += 1
                tmp.append(float(li[1]))
            index += 1
        while len(tmp) < 13:
            tmp.append(0)
        data.append(tmp)
    f.close()
    return np.mat(data), np.mat(label).T

if __name__ == "__main__":

    print("------ 1. load data")
    dataSet, labels = load_data_libsvm("heart_scale")

    print("------ 2. training")
    C = 0.6
    toler = 0.001
    maxIter = 500
    svm_model = svm.SVM_training(dataSet, labels, C, toler, maxIter)

    print("------ 3. cal accuracy")
    accuracy = svm.cal_accuracy(svm_model, dataSet, labels)
    print("The training accracy is %.3f%%" % (accuracy * 100))

    print("------ 4. save model")
    svm.save_svm_model(svm_model, "model_file")